package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsOrderChannelDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrder;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderChannel;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderChannelExample;
import com.ecoolex.chinadutyfree.retailer.pojo.DirectMailGoodsOrderChannelDTO;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

@Service
public class DirectMailGoodsOrderChannelManager {
    @Autowired
    private DirectMailGoodsOrderChannelDAO dao;



    public DirectMailGoodsOrderChannel save(DirectMailGoodsOrder order, DirectMailGoodsOrderChannelDTO channel, UnionPayPopularMerchant merchant, String createUser) {
        DirectMailGoodsOrderChannel record = new DirectMailGoodsOrderChannel();
        record.setChannelId(channel.getChannelId());
        record.setExternalUserId(channel.getExternalUserId());
        record.setChannelName(channel.getChannelName());
        record.setMerchantId(channel.getMerchantId());
        record.setOrderId(order.getOrderId());
        if(Check.notNull(merchant) && Check.notNullOrEmpty(merchant.getMerchantName())){
            record.setMerchantName(merchant.getMerchantName());
        }else {
            record.setMerchantName("");
        }
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setDeleted(DirectMailGoodsOrderChannel.NOT_DELETED);
        dao.insert(record);
        return record;
    }

    public DirectMailGoodsOrderChannel getChannelByOrderId(Integer orderId){
        DirectMailGoodsOrderChannelExample example = new DirectMailGoodsOrderChannelExample();
        DirectMailGoodsOrderChannelExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(DirectMailGoodsOrderChannel.NOT_DELETED)
                .andOrderIdEqualTo(orderId);
       return dao.selectOneByExample(example);

    }
}